package com.iseastar.guojiang.app;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.AppConfig;
import com.iseastar.guojiang.tools.JsCallback;
import com.shouhuobao.station.R;

import droid.frame.activity.title.TitleRes;

/**
 * 项目中用到的H5 相关一切挪用裹儿
 *
 * @author coffee<br>
 *         2015-7-27下午5:53:43
 */
public class WebViewActivity extends BaseActivity2 {

    protected WebView mWebView;
    private JsObject jsObject;
    private String title = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.webview);
        super.findViewById();

        title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");
        // setTitle(title);
        getAppTitle().setCommonTitle(title);

        if ("暂停营业".equals(title)) {
            url = "https://www.wenjuan.com/s/BvQNfa/";
        } else if ("用户使用条款".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/html/cal_terms.html";
        } else if ("帮助中心".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/html/cal_help/index.html";
            //            url = "http://10.100.0.71/html/cal_help/index.html";
        } else if ("驿站开通条款".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/html/cal_help/join.html";
        } else if ("获得推广码".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/html/cal_help/promo.html";
        } else if ("驿站开通".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/html/cal_help/stationLevel.html";
            getAppTitle().setCommonTitle(title, new TitleRes("跳过", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            }));
        } else if ("活动规则".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/promo/guide.html";
        } else if ("榜单活动预热公告".equals(title)) {
            url = AppConfig.getGuoErServerUrl() + "static/promo/warm.html";
        }
        initWebView(url);
        //
        if (jsObject != null) {
            mWebView.addJavascriptInterface(jsObject, "android");
        }
    }

    public class JsObject implements JsCallback {
        @JavascriptInterface
        public void gotoHome() {
            Intent data = new Intent();
            setResult(RESULT_OK, data);
            finish();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    protected void initWebView(String url) {
        mWebView = (WebView) findViewById(R.id.webview);
        // mWebView.setBackgroundColor(0); // 设置背景色
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 设置无缓存(防止访问静态file的时候出问题)
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 拨打电话
                if (url != null && url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                } else if (url != null) {
                    String scheme = Uri.parse(url).getScheme();//还需要判断host
                    if (TextUtils.equals("guoer", scheme)) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        WebViewActivity.this.finish();
                        return true;
                    }
                    return false;
                } else {
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                cancelLoadingDialog();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showLoadingDialog(null);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        if (url != null) {
            mWebView.loadUrl(url);
        }
        if (title.equals("帮助中心")) {
            WebChromeClient wvcc = new WebChromeClient() {
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    super.onReceivedTitle(view, title);
                    getAppTitle().setCommonTitle(title);
                }
            };
            // 设置setWebChromeClient对象
            mWebView.setWebChromeClient(wvcc);
        }
    }

    @Override
    public void finish() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.destroy();
        }
    }
}
